<template>
  <div>
    <h1>人员列表</h1>
    <h3>列表中第一个人员的名字是:{{ firstName }}</h3>
    <input type="text" v-model="name" placeholder="请输入人员名称" />
    <button @click="add">添加</button>
    <button @click="addWang">添加一个姓王的人</button>
    <button @click="addRandom">添加一个人，名字随机</button>
    <ul>
      <li v-for="person in personList" :key="person.id">
        {{ person.name }}
      </li>
    </ul>
    <!-- 组件间实现数据共享 -->
    <h3 style="color: green">当前求和为{{ sum }}</h3>
  </div>
</template>

<script>
import { nanoid } from 'nanoid';
// import {mapMutations} from "vuex"
export default {
  name: 'Person',
  data() {
    return {
      name: '',
    };
  },
  computed: {
    personList() {
      return this.$store.state.personConfig.personList;
    },
    sum() {
      return this.$store.state.countConfig.sum;
    },
    firstName() {
      return this.$store.getters['personConfig/firstName'];
    },
  },
  methods: {
    add() {
      const person = { id: nanoid(), name: this.name };
      // console.log(person);
      // this.$store.state.personList.unshift(person);
      this.$store.commit('personConfig/ADD_PERSON', person);
      this.name = '';
    },
    addWang() {
      const person = { id: nanoid(), name: this.name };
      this.$store.dispatch('personConfig/addWang', person);
      this.name = '';
    },
    addRandom() {
      this.$store.dispatch('personConfig/addRandom');
    },
  },
};
</script>

<style></style>
